package cn.jly.xstream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.security.AnyTypePermission;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.TimeZone;
import java.util.logging.Logger;

/**
 * 测试类在test目录下
 *
 * @PackageName cn.jly.xstream
 * @ClassName XStreamUtils
 * @Description xml和javaBean互转工具类
 * @Author 姬岚洋
 * @Date 2021/1/11 上午 11:13
 */
public class XStreamUtils {
    /**
     * logger
     */
    private static final Logger LOGGER = Logger.getLogger(XStreamUtils.class.getSimpleName());
    /**
     * xstream
     */
    private static final XStream X_STREAM;

    private static final DomDriver DOM_DRIVER =
            new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_"));

    private static final DateConverter DATE_CONVERTER =
            new DateConverter("yyyy-MM-dd HH:mm:ss", null, TimeZone.getTimeZone("GMT+8"));

    static {
        // xstream也提供了json的转换驱动，JettisonMappedXmlDriver和JsonHierarchicalStreamDriver。
        X_STREAM = new XStream(DOM_DRIVER);
        // XStream.setupDefaultSecurity(X_STREAM);
        // 设置最低安全防护
        X_STREAM.addPermission(AnyTypePermission.ANY);
        X_STREAM.registerConverter(DATE_CONVERTER);
    }

    public static XStream getxStream() {
        return X_STREAM;
    }

    public static <T> String toXml(T t) {
        // t为具体的根节点类，可以使用@XStreamAlias标注别名，也可以不用。
        X_STREAM.processAnnotations(t.getClass());
        return X_STREAM.toXML(t);
    }

    /**
     * 解析成xml文件
     *
     * @param <T>
     * @param t
     */
    public static <T> void toXmlFile(T t, String filePath) {
        X_STREAM.processAnnotations(t.getClass());
        try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
            X_STREAM.toXML(t, fileOutputStream);
        } catch (IOException e) {
            LOGGER.severe(String.format("转换出错,出错原因为: %s", e.getMessage()));
            e.printStackTrace();
        }

    }

    /**
     * 将xml解析成javaBean
     *
     * @param xmlText xml字符串
     * @param targetClazz
     * @param <T>
     * @return
     */
    public static <T> T fromXml(String xmlText, Class<T> targetClazz) {
        X_STREAM.processAnnotations(targetClazz);
        return (T) X_STREAM.fromXML(xmlText);
    }

    /**
     * 将xml解析成javaBean
     *
     * @param file
     * @param targetClazz
     * @param <T>
     * @return
     * @throws FileNotFoundException
     */
    public static <T> T fromXml(File file, Class<T> targetClazz) throws FileNotFoundException {
        if (file == null) {
            String msg = "xml文件不能为空";
            LOGGER.severe(msg);
            throw new FileNotFoundException(msg);
        }

        X_STREAM.processAnnotations(targetClazz);
        return (T) X_STREAM.fromXML(file);
    }

    private XStreamUtils() {
    }
}
